NProgress.start();
window.onload = function () {
    NProgress.done();
}

function generateMenuItem(root, index) {
    var htmlStr =
        // '<li class="layui-nav-item ' + (index == 0 ? 'layui-nav-itemed' : '') + '">' +
        '<li class="layui-nav-item">' +
        '   <a class="" href="javascript:;">' +
        '       <i class="iconfont ' + root.icon + '"></i>' + root.name +
        '   </a>' +
        generateLeaf(root, index) +
        '</li>';
    return htmlStr;
}

function generateLeaf(parentMenu, index) {
    var children = parentMenu.children;
    var htmlStr = '';
    if (children !== undefined && children != null) {
        htmlStr =
            '<dl class="layui-nav-child">';
        for (var i = 0; i < children.length; i++) {
            var menu = children[i];
            htmlStr +=
                '<dd>' +
                '           <a href="javascript:;" path="' + menu.url + '" tab-id="' + (index + 1) + '-' + (i + 1) + '"><i class="iconfont ' + menu.icon + '"></i> ' + menu.name + '</a>'
                + generateLeaf(menu) +
                '</dd>';
        }
        htmlStr +=
            '</dl>';
    }
    return htmlStr;
}

layui.use(['element', 'layer'], function () {
    var layer = layui.layer;
    var $ = layui.jquery;
    var element = layui.element;

    $.ajax({
        url: "menu/tree",
        dataType: "json",
        method: "get",
        async: false,
        success: function (tree) {
            var treeStr = '';
            if (tree != null) {
                for (var i = 0; i < tree.length; i++) {
                    treeStr += generateMenuItem(tree[i], i);
                }
            }
            $("#menuBox").empty();
            $("#menuBox").append(treeStr);

            var layFilter = $("#nav").attr('lay-filter');
            element.render('nav', layFilter);
        },
        error: function () {

        }
    });

    /**
     * 左边菜单显示/隐藏功能
     * @type {boolean}
     */
    $(".menu-switch").click(function () {
        if ($(".layui-layout-admin .layui-side").css("left") == '0px') {
            $(".layui-layout-admin .layui-side").animate({left: "-200px"});
            $(".layui-layout-admin .content-body").animate({left: "0px"});
            $(".layui-layout-admin .layui-footer").animate({left: "0px"});
        } else {
            $(".layui-layout-admin .layui-side").animate({left: "0px"});
            $(".layui-layout-admin .content-body").animate({left: "200px"});
            $(".layui-layout-admin .layui-footer").animate({left: "200px"});
        }
    });

    /**
     * 点击左边菜单在右边添加选项卡
     */
    $(".layui-nav-child").find("dd").click(function () {
        // 纯文字
        // var title = $(this).text();
        // 图标+文字
        var title = $(this).find("a").html();
        var path = $(this).children('a').attr('path');
        var tabId = $(this).children('a').attr('tab-id');
        // 去重复选项卡
        for (var i = 0; i < $('.ok-frame').length; i++) {
            if ($('.ok-frame').eq(i).attr('tab-id') == tabId) {
                element.tabChange("ok-tab", tabId);
                event.stopPropagation();
                return;
            }
        }
        // 添加选项卡
        element.tabAdd("ok-tab", {
            title: title,
            content: "<iframe src='" + path + "' tab-id='" + tabId + "' class='ok-frame' frameborder='0' scrolling='yes' width='100%' height='100%'></iframe>",
            id: tabId
        });
        // 切换选项卡
        element.tabChange("ok-tab", tabId);
    });

    /**
     * 修改copyright结束时间
     */
    var data = new Date();
    var year = data.getFullYear();
    $("#endYear").text(year);

    /**
     * 退出操作
     */
    $("#logout").click(function () {
        layer.confirm("确定要退出吗？", {skin: 'layui-layer-lan', icon: 3, title: '提示', anim: 6}, function () {
            $.ajax({
                url : "logout",
                method : "get",
                dataType : "json",
                success : function (result) {
                    if(result.success){
                        location.href = "login";
                    }else{
                        layer.msg("退出失败, 请稍后重试.", {icon: 6, time: 1000, anim: 4});
                    }
                },
                error : function(){
                    layer.msg("退出失败, 请稍后重试.", {icon: 6, time: 1000, anim: 4});
                }
            });
        });
    });

    /**
     * 锁定账户
     */
    $("#lock").click(function () {
        layer.confirm("确定要锁定账户吗？", {skin: 'layui-layer-lan', icon: 4, title: '提示', anim: 1}, function (index) {
            layer.close(index);
            $(".yy").show();
            layer.prompt({
                btn: ['确定'],
                title: '输入密码解锁(123456)',
                closeBtn: 0,
                formType: 1
            }, function (value, index, elem) {
                if (value == "123456") {
                    layer.close(index);
                    $(".yy").hide();
                } else {
                    layer.msg('密码错误', {anim: 6});
                }
            });
        });
    });

    $(".layui-tab-title").on('contextmenu', 'li', function(event) {
        var tab_left = $(this).position().left;
        var tab_width = $(this).width();
        var left = $(this).position().top;
        var this_index = $(this).attr('lay-id');
        $('#tab_right').css({'left':tab_left+tab_width/2}).show().attr('lay-id',this_index);
        $('#tab_show').show();
        return false;
    });

    $('#tab_right').on('click', 'dd', function(event) {
        var data_type = $(this).attr('data-type');
        var lay_id = $(this).parents('#tab_right').attr('lay-id');
        if(data_type=='this'){
            $('.layui-tab-title li[lay-id='+lay_id+']').find('.layui-tab-close').click();
        }else if(data_type=='other'){
            $('.layui-tab-title li').eq(0).find('.layui-tab-close').remove();
            $('.layui-tab-title li[lay-id!='+lay_id+']').find('.layui-tab-close').click();
        }else if(data_type=='all'){
            $('.layui-tab-title li[lay-id]').find('.layui-tab-close').click();
        }
        $('#tab_right').hide();
        $('#tab_show').hide();
    })


    $('.leftregion, .topregion, .contentregion, .contentinner, #tab_show').click(function(event) {
        $('#tab_right').hide();
        $('#tab_show').hide();
    });
});
